/*
 * KeyboardManager.java
 *
 * Created on 15 de octubre de 2006, 1:55
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ub.es.ubictionary.logic;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import ub.es.ubictionary.io.Reader;
import ub.es.ubictionary.io.Writer;

/**
 *
 * @author aram
 */
public class KeyboardManager {
    
    ArrayList keyboards;
    /** Creates a new instance of KeyboardManager */
    public KeyboardManager() {
        keyboards = new ArrayList(5);
    }
    
    public void loadKeyboards() throws FileNotFoundException, UnsupportedEncodingException, IOException{
        ArrayList lst = new ArrayList();
        lst = Reader.getLines("keyboard");
        for (int i = 0; i < lst.size(); i++){
            String l = (String) lst.get(i);
            String [] ls = l.split("#", 2);
            if (ls.length > 1){
                String [] keys = ls[1].split("\\|");
                keyboards.add(new Keyboard(ls[0], keys));
            }
        }
    }
    
    public ArrayList getKeyboards(){
        return keyboards;
    }

    public Keyboard getKeyboard(String name) {
        for(int i = 0; i < keyboards.size(); i++){
            if ( ((Keyboard)keyboards.get(i)).getName().compareTo(name) ==0 ){
                return (Keyboard) keyboards.get(i);
            }
        }
        return null;
    }
    
    public int getCuantityOfKeyboards(){
        return keyboards.size();
    }

    public boolean deleteKeyboard(String name) {
        Keyboard k = getKeyboard(name);
        if (k==null)
            return false;
        keyboards.remove(k);
        return saveKeyboardsOnFile();
    }

    private boolean saveKeyboardsOnFile() {
        ArrayList ar = new ArrayList();
         for (int i = 0; i < keyboards.size(); i++){
             String salida = "";
             Keyboard keyboard = (Keyboard) keyboards.get(i);
             salida = keyboard.getName() + "#";
             String w[] = keyboard.getKeys();
             for (int j = 0; j < w.length; j++){
                 if (j == w.length-1){
                     salida = salida + w[j];
                 }else{
                     salida = salida + w[j] + "|";
                 }
             }
             ar.add(salida);
         }
        try {
            Writer.createFile("keyboard",ar);
        } catch (UnsupportedEncodingException ex) {
            return false;
        } catch (FileNotFoundException ex) {
            return false;
        } catch (IOException ex) {
            return false;
        }
        return true;
    }

    public Keyboard openKeyboard(String file) throws FileNotFoundException, UnsupportedEncodingException, IOException, InvalidKeyboardFileException {
        ArrayList lst = new ArrayList();
        lst = Reader.getLinesFromExternalFile(file);
        if (lst.size() > 0){
            String l = (String) lst.get(0);
            String [] ls = l.split("#", 2);
            if (ls.length > 1){
                String [] words = ls[1].split("\\|");
                return new Keyboard(ls[0], words);
            }else{
                throw new InvalidKeyboardFileException();
            }
        }else{
            throw new InvalidKeyboardFileException();
        }
    }

    public boolean addKeyboard(String name, String[] keys) {
        Keyboard l = getKeyboard(name);
        if (l != null)
            l.setKeys(keys);
        else
            keyboards.add(new Keyboard(name, keys));
        
        return saveKeyboardsOnFile();
    }
    
}
